<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>.net Windows服务程序和安装程序制作图解 - しovの沉没 - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=Jsknfrse2BShd-7DrxM42RyulObn44e4JjkI6-WX6zk1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/newyear2009/bundle-newyear2009.css?v=h0kR74NknBiuP_WcZEhM_s2ggKDYne2nevFgAUohsYI1"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/Andy_Qin/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/Andy_Qin/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/Andy_Qin/wlwmanifest.xml"/>
<script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'Andy_Qin', cb_enable_mathjax=false;</script>
<script src="/bundles/blog-common.js?v=V3tkfsuahqfUzgmqZVwj2QjsxodIvgDfvPKdn8yDuMY1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>

<!--done-->
<div id="home" class="hui-d-custom"> 
<div id="header"  class="hui-d3">
	<div id="blogTitle">
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/Andy_Qin/">しovの沉没</a></h1>
<h2></h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->

	<div id="navigator">
	<div class="hui-d2"> 
		
<!--done-->
<ul id="navList">
	<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</a></li>
	<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/Andy_Qin/">首页</a></li>
	<li><a href="http://space.cnblogs.com/" cssclass="menu">社区</a></li>
	<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="http://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
	<li><a id="MyLinks1_ContactLink" accesskey="9" class="menu" rel="nofollow" href="http://msg.cnblogs.com/send/%E3%81%97ov%E3%81%AE%E6%B2%89%E6%B2%A1">联系</a></li>
	<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="http://i.cnblogs.com/">管理</a></li>
	<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/Andy_Qin/rss">订阅</a>
	<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/Andy_Qin/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a></li>
</ul>


		<div class="blogStats">
			
			
<!--done-->
随笔- 4&nbsp;
文章- 1&nbsp;
评论- 30&nbsp;

			
		</div><!--end: blogStats -->
	</div>
</div><!--end: navigator 博客导航栏 -->

</div><!--end: header -->


<div class="hui-d3 hui-t5" id="body">
	<div class="hui-main"> 
		<div class="hui-b"> 
		
<div id="post_detail">
<!--done-->
<div id="topics">
	<div class = "post">
		<h1 class = "h1PostTitle">
			<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/Andy_Qin/archive/2010/01/21/1653548.html">.net Windows服务程序和安装程序制作图解</a>
		</h1>
		<div class="postBody">
			<div id="cnblogs_post_body"><h3 align="center"><span style="color: #808080">.net Windows服务程序和安装程序制作</span></h3>
<p><span style="font-size: 10pt; color: #808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;最近项目中用到window服务程序，以前没接触过，比较陌生，花了两天的时间学习了下，写了个简单的服务，但在制作安装程序的时候，参照网上很多资料，却都制作不成功，可能是开发环境或项目配置的不同，这里把自己成功制作的方式贴出来，一方面留给自己，一方面也希望能给遇到同样问题绝望中不能自拔、以致失去灵魂的路人甲，路人乙一还魂丹&nbsp;. . .</span></p>
<p><span style="font-size: 10pt; color: #808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDE ：WIN2003 &nbsp;VS2008&nbsp; SQL2005</span></p>
<p><span style="font-size: 10pt; color: #808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一、创建window服务项目</span></p>
<p><span style="font-size: 10pt; color: #808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 首先我们建一个window服务项目 ：解决方案（右击）&#8212;&#8212;&gt; 增加 &#8212;&#8212;&gt; 新建项目&#8212;&#8212;&gt;项目类型选择Windows&#8212;&#8212;&gt;模板选择Windows服务 ，如图： 
<div align="center"><img height="396" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic01.jpg" width="676" border="0" /></div></span>
<p>&nbsp;</p>
<p><span style="font-size: 10pt; color: #808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 二、编写Window服务程序</span></p>
<p><span style="font-size: 10pt; color: #808080">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创建后会生成两个文件 Program.cs 和 Service1.cs ，其中 program.cs 这个文件不用动，里面已自动生成好了。我们在Service1.cs里增加所要的操作，里面有两个重写方法 OnStart 和 OnStop ，OnStart 方法表示服务启动的操作，OnStop 表示服务停止的操作，下面写了一个简单示例 ：当服务启动后，调用远程一个WebService的Hello函数，并把返回的值写入到一个文件中。code如下：</p>
<div class="cnblogs_code" onclick="cnblogs_code_show('51a804a5-696c-4f3a-aacf-b0f1cb04793d')"><img class="code_img_closed" id="code_img_closed_51a804a5-696c-4f3a-aacf-b0f1cb04793d" alt="" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" /><img class="code_img_opened" id="code_img_opened_51a804a5-696c-4f3a-aacf-b0f1cb04793d" style="display: none" onclick="cnblogs_code_hide('51a804a5-696c-4f3a-aacf-b0f1cb04793d',event)" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif"><span class="cnblogs_code_collapse">代码</span> 
<div class="cnblogs_code_hide" id="cnblogs_code_open_51a804a5-696c-4f3a-aacf-b0f1cb04793d">
<div><!--<br/ />
<br/ />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />
http://www.CodeHighlighter.com/<br/ />
<br/ />
--><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;summary&gt;</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;服务启动的操作<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;/summary&gt;</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;param&nbsp;name="args"&gt;&lt;/param&gt;</span><span style="color: #808080"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">protected</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">override</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;OnStart(</span><span style="color: #0000ff">string</span><span style="color: #000000">[]&nbsp;args)&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartHello();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;summary&gt;</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;服务停止的操作<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;/summary&gt;</span><span style="color: #808080"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">protected</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">override</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;OnStop()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThreadHello.Abort();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flag&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">线程停止</span><span style="color: #800000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">catch</span><span style="color: #000000">&nbsp;(Exception&nbsp;ex)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Trace.Write(ex.Message);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;Thread&nbsp;ThreadHello;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;StartHello()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;标准形式<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">ThreadStart&nbsp;NewThreadStart&nbsp;=&nbsp;new&nbsp;ThreadStart(VoidName);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">Thread&nbsp;NewThead&nbsp;=&nbsp;new&nbsp;Thread(NewThreadStart);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">NewThead.Start();</span><span style="color: #008000"><br /></span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThreadHello&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Thread(</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;ThreadStart(Hello));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThreadHello.Start();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">线程任务开始</span><span style="color: #800000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">catch</span><span style="color: #000000">&nbsp;(Exception&nbsp;ex)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Trace.Write(ex.Message);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">throw</span><span style="color: #000000">&nbsp;ex;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Hello()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(Flag)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;localhost.AdvService&nbsp;la&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;WindowsService1.localhost.AdvService();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;helloname&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;la.Hello(</span><span style="color: #800000">"</span><span style="color: #800000">Andy</span><span style="color: #800000">"</span><span style="color: #000000">);&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;调用远程WebService中的方法</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeInLog(helloname,&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;把调用返回的数据写入到文件中</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">catch</span><span style="color: #000000">&nbsp;(Exception&nbsp;ex)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Diagnostics.Trace.Write(ex.Message);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">throw</span><span style="color: #000000">&nbsp;ex;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.Sleep(</span><span style="color: #800080">5000</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;summary&gt;</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;写入文件操作<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;/summary&gt;</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;param&nbsp;name="msg"&gt;</span><span style="color: #008000">写入内容</span><span style="color: #808080">&lt;/param&gt;</span><span style="color: #008000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080">///</span><span style="color: #008000">&nbsp;</span><span style="color: #808080">&lt;param&nbsp;name="IsAutoDelete"&gt;</span><span style="color: #008000">是否删除</span><span style="color: #808080">&lt;/param&gt;</span><span style="color: #808080"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;writeInLog(</span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;msg,&nbsp;</span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;IsAutoDelete)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">try</span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;logFileName&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">@"</span><span style="color: #800000">E:\Sample\PG_Sample\log.txt</span><span style="color: #800000">"</span><span style="color: #000000">;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;文件路径</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInfo&nbsp;fileinfo&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;FileInfo(logFileName);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(IsAutoDelete)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(fileinfo.Exists&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;fileinfo.Length&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #800080">1024</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileinfo.Delete();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;(FileStream&nbsp;fs&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;fileinfo.OpenWrite())<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StreamWriter&nbsp;sw&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;StreamWriter(fs);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.BaseStream.Seek(</span><span style="color: #800080">0</span><span style="color: #000000">,&nbsp;SeekOrigin.End);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">=====================================</span><span style="color: #800000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Write(</span><span style="color: #800000">"</span><span style="color: #800000">添加日期为:</span><span style="color: #800000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;DateTime.Now.ToString()&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">"</span><span style="color: #800000">\r\n</span><span style="color: #800000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Write(</span><span style="color: #800000">"</span><span style="color: #800000">日志内容为:</span><span style="color: #800000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;msg&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">"</span><span style="color: #800000">\r\n</span><span style="color: #800000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">=====================================</span><span style="color: #800000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Flush();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">catch</span><span style="color: #000000">&nbsp;(Exception&nbsp;ex)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.ToString();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span></div></div></div>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 例子很简单，方便理解，上面通过线程，每5秒执行一次操作，到这里，我们的服务以及服务实现的功能就已经写好了，下面怎么让客户端安装我们的服务呢？这里才是我写这篇文章的目的！先要增加一个安装程序类，切换到视图窗口，右击&#8212;&#8212;&gt;增加安装程序，便会增加一个程序安装类文件 ProjectInstaller.cs，如图：</p>
<p align="center"><img height="275" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic10.jpg" width="724" border="0" /></p>
<p align="left">这里serviceProcessInstaller1 和 serviceInstaller1 有几个重要属性要设置，serviceInstaller1 中的StartType要设置成Automatic，表示随机启动，ServiceName表示服务名称，Description 表示服务的描述， DisplayName 表示显示名称。serviceProcessInstaller1 中的Account要设置成LocalSystem,表示本地系统帐号 </p>
<p align="left">
<div align="center"><img height="155" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic11.jpg" width="404" border="0" /></div></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Window服务的安装有两个方式：</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一、命令安装</p>
<div align="center"><img height="178" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic2.jpg" width="647" border="0" />&nbsp;</div>
<div align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 通过命令窗口，执行 InstallUtil.exe 服务路径 可安装Window服务，你是不是觉得我在扯，这么长的命令怎么看？怎么记住？可以想想如果客户端是一个不懂程序的人是不可能自己完成安装的，那么能不能制作一个简单有效，点点按钮可以安装的呢，答案是肯定的 &#8212;&#8212;&gt; 安装程序制作</div>
<div align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 二、安装程序</div>
<div align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;安装程序制作 ：解决方案（右击）&#8212;&#8212;&gt; 增加 &#8212;&#8212;&gt; 新建项目 &#8212;&#8212;&gt; 项目类型选择安装和部署 &#8212;&#8212;&gt; &nbsp;模板选择安装项目&nbsp;&nbsp; 如图&nbsp;：</div>
<div align="center"><img height="397" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic3.jpg" width="679" border="0" /></div>
<div align="left">&nbsp;</div>
<div align="left">&nbsp;&nbsp;&nbsp;&nbsp; 创建后，安装项目（右击） &#8212;&#8212;&gt; 视图 &#8212;&#8212;&gt; 文件系统 如图：</div>
<div align="center"><img height="124" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic4.jpg" width="979" border="0" /></div>
<div align="left">&nbsp;</div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;由于我们是安装服务，就不需要用户桌面和程序菜单了，直接应用程序文件夹（右击）&#8212;&#8212;&gt; 增加 &#8212;&#8212;&gt; 项目输出 入图 ：</p>
<p align="center"><img height="406" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic5.jpg" width="553" border="0" /></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 上面有两点需要注意 ，一 项目选择中选择要安装的服务， 二 项目选择下面要选择主输出，选择好后确定，这一步增加了安装程序的文件夹，下一步就是给这个安装程序增加操作，这里我们增加两个基本操作，一个是安装，一个是卸载。安装项目（右击） &#8212;&#8212;&gt; 视图 &#8212;&#8212;&gt;&nbsp;自定义操作 如图</p>
<p align="center"><img height="210" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic6.jpg" width="398" border="0" /></p>
<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 上面可以看到有安装，提交，回滚，卸载等操作，我们先增加安装操作，安装（右击）&#8212;&#8212;&gt; 增加自定义操作，会弹出一个对话 如图：</p>
<p align="center"><img height="394" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic7.jpg" width="613" border="0" /></p>
<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;选择应用程序文件夹，并选中之前增加的主输出项，确定，这样这个安装程序就增加了安装的操作，同样按照这样的方式增加卸载操作，卸载与安装唯一不同的是需要设置一个命令参数，不可少，如图：</p>
<p align="center"><img height="170" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic8.jpg" width="749" border="0" /></p>
<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这里 Arguments 里输入 /u&nbsp; 表示卸载命令相当于 InstallUtil.exe&nbsp;/u&nbsp;服务路径&nbsp; , 到这里 ，我们的安装程序就已经制作好了，生成安装程序项目，将会生成 setup.exe 和 setup.msi 安装文件，拷贝到客户端，点击setup.exe 就像安装QQ一样安装我们的服务，实际上比QQ好多了，QQ还有广告，我们可是纯绿色的！</p>
<p align="left">
<div align="center"><img height="410" alt="" src="http://images.cnblogs.com/cnblogs_com/andy_qin/pic9.jpg" width="602" border="0" /></div></p></span></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


		</div>
		<div class = "postDesc">posted @ <span id="post-date">2010-01-21 18:21</span> <a href='http://www.cnblogs.com/Andy_Qin/'>しovの沉没</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="http://i.cnblogs.com/EditPosts.aspx?postid=1653548" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(1653548);return false;">收藏</a> </div>
	</div>
	<script type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=51075,cb_entryId=1653548,cb_blogApp=currentBlogApp,cb_blogUserGuid='fef116b0-1cf5-dd11-9e4d-001cf0cd104b',cb_entryCreatedDate='2010/1/21 18:21:00';loadViewCount(cb_entryId);</script>
	
</div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>刷新评论</a><a href='#' onclick='return RefreshPage();'>刷新页面</a><a href='#top'>返回顶部</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='ad_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
$(function () {
    setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
    deliverAdT2();
    deliverAdC1();    
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);    
});
</script>
</div>


		</div><!--end: hui-main hui-b -->
	</div><!--end: hui-main -->
	<div class="hui-b"> 
	<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="calendar"><div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script></div>
			
			<DIV id="leftcontentcontainer">
				<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
			</DIV>
			
		</div><!--end: sideBarMain -->
	</div><!--end: hui-b -->
	<div class="clear"></div>
</div>

<div id="footer" class="hui-d3">
		
<!--done-->
Copyright &copy;2016 しovの沉没
</div><!--end: footer -->

</div><!--end: home 自定义的最大容器 -->
</body>
</html>
